package com.photo.tagger;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ImageMetadataDBAdapter {
	public static final String ROW_ID = "ID";
	public static final String IMAGE_URI = "image_uri";
	public static final String NO_OF_TAGS = "no_of_tags";

	private static final String DATABASE_NAME = "TagDB";
	private static final String DATABASE_TABLE = "imageMetadata";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table"
			+ DATABASE_TABLE + " (" + ROW_ID
			+ "integer primary key autoincrement, " + IMAGE_URI
			+ " text not null, " + NO_OF_TAGS + " integer not null);";

	private Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public void DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	public class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		public DatabaseHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase arg0) {
			// TODO Auto-generated method stub
			try {
				db.execSQL(DATABASE_CREATE);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase arg0, int oldVersion,
				int newVersion) {
			// TODO Auto-generated method stub
			Log.w("tag", "Upgrading database from version " + oldVersion
					+ " to " + newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}

	}

	public ImageMetadataDBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		DBHelper.close();
	}

	public long insertImageMetadata(int number, String imageUri) {
		ContentValues content = new ContentValues();
		content.put(NO_OF_TAGS, number);
		content.put(IMAGE_URI, imageUri);
		return db.insert(DATABASE_TABLE, null, content);
	}

	public boolean deleteImageMetadata(String ImageUri) {
		return db.delete(DATABASE_TABLE, IMAGE_URI + "=" + ImageUri, null) > 0;
	}

	public Cursor getImageMetaData(String ImageUri) throws SQLException {
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { ROW_ID,
				NO_OF_TAGS, IMAGE_URI }, IMAGE_URI + "=" + ImageUri, null,
				null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	public boolean updateImageMetadata(int number, String imageUri) {
		ContentValues content = new ContentValues();
		content.put(NO_OF_TAGS, number);
		content.put(IMAGE_URI, imageUri);
		return db.update(DATABASE_TABLE, content, IMAGE_URI +"="+ imageUri,null)>0;
		
		
	}
	public boolean updateImageMetadata(int id,int number, String imageUri) {
		ContentValues content = new ContentValues();
		content.put(NO_OF_TAGS, number);
		content.put(IMAGE_URI, imageUri);
		return db.update(DATABASE_TABLE, content, ROW_ID+"="+ id,null)>0;
		
		
	}
	/*
	 * public static final String ROW_ID = "ID"; public static final String
	 * IMAGE_URI = "image_uri"; public static final String NO_OF_TAGS =
	 * "no_of_tags";
	 * 
	 * 
	 * private static final String DATABASE_NAME = "TagDB"; private static final
	 * String DATABASE_TABLE = "imageMetadata"; private static final int
	 * DATABASE_VERSION = 1;
	 * 
	 * private static final String DATABASE_CREATE =
	 * "create table"+DATABASE_TABLE
	 * +" ("+ROW_ID+"integer primary key autoincrement, "
	 * +IMAGE_URI+" text not null, "+NO_OF_TAGS+ " integer not null);";
	 * 
	 * private final Context context; //private DatabaseHelper DBHelper; private
	 * SQLiteDatabase db;
	 */

}
